package com.lry.basic.netty.demo2;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.*;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;

/**
 * @author:刘仁有
 * @desc:
 * @email:953506233@qq.com
 * @data:2019/9/11
 */
public class Client {
    private String host;
    private int port;

    public Client(String host,int port){
        this.host = host;
        this.port = port;
    }
    //FixedLengthFrameDecoder 定长
    //LineBasedFrameDecoder  换行
    //LengthFieldBasedFrameDecoder 自定义变长
    //DelimiterBasedFrameDecoder 特殊分隔符
    public static void main(String[] args) throws InterruptedException {
        new Client("localhost",9999).start();
    }
    public void start()throws InterruptedException{
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            ChannelPipeline pipeline = socketChannel.pipeline();
//                            ByteBuf delimiter = Unpooled.copiedBuffer("$".getBytes("UTF-8"));
//                            pipeline.addLast("frame",new DelimiterBasedFrameDecoder(2048, delimiter));
                            //pipeline.addLast(new LineBasedFrameDecoder(100));//配合future.channel().writeAndFlush(input.readLine()+"\r\n");
//                            pipeline.addLast(new LengthFieldBasedFrameDecoder(1024,0,4,0,4));
                            socketChannel.pipeline().addLast( new LengthFieldPrepender(4,false));
                            pipeline.addLast("decode",new StringDecoder());//解码器
                            pipeline.addLast("encode",new StringEncoder());
                            pipeline.addLast("handler", new SimpleChannelInboundHandler<String>() {
                                @Override
                                protected void channelRead0(ChannelHandlerContext ctx, String s) throws Exception {
                                    //直接输出消息
                                    System.out.println(s);
                                }
                            });
                        }
                    });
            ChannelFuture future = b.connect(host,port).sync();

            BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
            while (true){
                String inputStr = input.readLine();
                future.channel().writeAndFlush(inputStr.length());
                future.channel().writeAndFlush(inputStr);

                future.channel().writeAndFlush(5);
                future.channel().writeAndFlush("hello");

                future.channel().writeAndFlush(5);
                future.channel().writeAndFlush("world");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            group.shutdownGracefully().sync();
        }
    }
}
